window.parent.Ext.QuickTips.init();
var pwdChanServlet = '../../servlet/system/PwdChange';
//改密码

var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv"
        + "wxyz0123456789+/" + "=";
function encode64(input) {

    var output = "";
    var chr1, chr2, chr3 = "";
    var enc1, enc2, enc3, enc4 = "";
    var i = 0;
    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }
        output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
                + keyStr.charAt(enc3) + keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
    } while (i < input.length);

    return output;
}

var pwdWin = null;
window.parent.Ext.apply(window.parent.Ext.form.VTypes, {
    password: function(val, field) {
        if (field.initialPassField) {
            var pwd = window.parent.Ext.getCmp(field.initialPassField);
            return (val === pwd.getValue());
        }
        return true;
    },
    passwordText: '两次输入的密码不一致'
});
var changeForm = new window.parent.Ext.FormPanel({
    xtype: 'form',
    id: 'pwd-form',
    bodyStyle: 'padding:15px;background:transparent',
    frame: true,
    border: false,
    url: 'ChangePasswordServlet',
    items: [{
            id: 'oldpwd',
            //name: 'oldpwd',
            xtype: 'textfield',
            allowBlank: false,
            inputType: 'password',
            //vtype: 'password',
            fieldLabel: '原密码',
            blankText: '密码必须填写'
        }
        , {
            id: 'newpwd',
            //name: 'newpwd',
            xtype: 'textfield',
            inputType: 'password',
            fieldLabel: '新密码',
            allowBlank: false,
            blankText: '密码必须填写',
            minLength: 8,
            minLengthText: '密码长度必须介于8到20之间',
            maxLength: 20,
            maxLengthText: '密码长度必须介于8到20之间'
            //regex: /^([0-9]+)$/,
            //regexText: "密码必须同时包括数字和字母,最小长度为6,最大长度25"
        }, {
            id:'confirmpwd',
            name: 'confirmpwd',
            xtype: 'textfield',
            allowBlank: false,
            inputType: 'password',
            vtype: 'password',
            initialPassField: 'newpwd',
            fieldLabel: '新密码确认',
            minLength: 8,
            minLengthText: '密码长度必须介于8到20之间',
            maxLength: 20,
            maxLengthText: '密码长度必须介于8到20之间'
            //regex: /^([0-9]+)$/,
            //regexText: "密码必须同时包括数字和字母,最小长度为6,最大长度25"
        }
    ]});
var pwdWin = new window.parent.Ext.Window({
    layout: 'form',
    width: 340,
    modal: true,
    autoHeight: true,
    closeAction: 'hide',
    items: [changeForm],
    loadMask: true,
    buttons: [{
            text: '保存',
            //iconCls: 'icon-save',
            handler: function() {
                pwdWin.hide();
                var pwdMask = new  window.parent.Ext.LoadMask(window.parent.Ext.getBody(), {msg:"正在操作，请稍后..."});	
                pwdMask.show(); 
                var conn = new window.parent.Ext.data.Connection();                
                var oldPwd = window.parent.Ext.getCmp("oldpwd").getValue();                
                var newPwd =window.parent.Ext.getCmp('newpwd').getValue();
                oldPwd = encode64(oldPwd);
                newPwd = encode64(newPwd);
                conn.request({
                    url: pwdChanServlet,
                    params: {pa33: oldPwd, pa44: newPwd},
                    method: 'post',
                    scope: this,
                    callback: function(options, success, response) {
                        pwdMask.hide();
                        if (success) {
                            var messager = window.parent.Ext.decode(response.responseText);
                            if (messager.msg === '') {
                                window.parent.Ext.MessageBox.alert("提示", "修改密码成功");
                            }
                            else
                                window.parent.Ext.MessageBox.alert('提示', messager.msg);
                        } else
                            window.parent.Ext.MessageBox.alert("提示", "修改密码失败！");

                    }

                });
            }
        },
        {
            text: '取消',
            handler: function() {
                pwdWin.hide();
            }
        }]
});
function changePassword() {
    changeForm.getForm().reset();
    //Ext.getCmp('pwd-form').getForm().reset();
    pwdWin.setTitle('修改密码');
    pwdWin.show();
}
